# -*- coding: utf-8 -*-
############################################################################
from chee.shortcuts import *
############################################################################
class Json_User_Info(JsonHandler):
    rule = Rule('/json/user/')
    def post(self):
        try:
            from chee.models import User_Info
            user = self.request.user
            retobj = {"id":user.id,"name":user.name,"nick":user.nick_name,"gold":user.gold,"bean":user.bean}
            return JsonMsg(True,u'user',retobj)
        except: return JsonMsg(False,u'error',u'exception')
class Json_User_Info_Other(JsonHandler):
    rule = Rule('/json/user/<int:user_id>/')
    def post(self):
        try:
            from chee.models import User_Info
            user_id = self.kwargs['user_id']
            user = store.find(User_Info,User_Info.id==user_id).one()
            if not user: return JsonMsg(False,u'error',u'no user')
            retobj = {"id":user.id,"name":user.name,"nick":user.nick_name,"gold":user.gold}
            return JsonMsg(True,u'user',retobj)
        except: return JsonMsg(False,u'error',u'exception')
class Json_User_Friend(JsonHandler):
    rule = Rule('/json/user/friend/')
    def post(self):
        try:
            from chee.models import User_Info,User_Friend
            user_id = int(self.request.form.get('userid','').strip())
            tbs = [User_Info,Join(User_Friend.friend_id==User_Info.id)]
            friends = store.using(*tbs).find(User_Info,User_Friend.owner_id==user_id).order_by(Asc(User_Friend.id))
            retobj = []
            for user in friends:
                uobj = {"id":user.id,"name":user.name,"nick":user.nick_name,"gold":user.gold,"bean":user.bean}
                retobj.append(uobj)
            return JsonMsg(True,u'user.friend',retobj)
        except: return JsonMsg(False,u'error',u'exception')
class Json_User_Mail(JsonHandler):
    rule = Rule('/json/user/mail/')
    def post(self):
        try:
            from chee.models.user import User_Mail,User_Info
            type = int(self.request.form.get('type','unread').strip())
            page = int(self.request.form.get('page','1').strip())
            conditions = [User_Mail.owner_id==user_id]
            if type == 'unread': conditions.append(User_Mail.is_read==0)
            elif type == 'recv': conditions.append(User_Mail.to_id==self.request.user.id)
            tbs = [User_Info,Join(User_Mail,User_Mail.from_id==User_Info.id)]
            mails = store.using(*tbs).find((User_Info,User_Mail),*conditions).order_by(Desc(User_Mail.send_time))
            if page > 1: mails = mail.config(limit=page_count,offset=page_count*page)
            retobj = []
            for (user,mail) in mails:
                mobj = {'from_id':mail.from_id,'from_name':user.nick_name,'title':mail.title,'content':mail.content,'send_time':mail.send_time}
                retobj.append(mobj)
            return JsonMsg(True,u'user.mail',retobj)
        except: return JsonMsg(False,u'error',u'exception')
